<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Vue全栈开发美团网,">










<meta name="description" content="本文为《Vue全家桶+SSR+Koa2全栈开发美团网》的学习笔记的第二章，主要内容为Vue基础知识视频地址：https://coding.imooc.com/class/280.html vue官网地址：https://cn.vuejs.org/">
<meta name="keywords" content="Vue全栈开发美团网">
<meta property="og:type" content="article">
<meta property="og:title" content="【第二章】Vue全家桶+SSR+Koa2全栈开发美团网">
<meta property="og:url" content="https://aiolos123.gitee.io/blog/2019/09/02/full-stack-dev-meituan-with-vue-two/index.html">
<meta property="og:site_name" content="Aiolos">
<meta property="og:description" content="本文为《Vue全家桶+SSR+Koa2全栈开发美团网》的学习笔记的第二章，主要内容为Vue基础知识视频地址：https://coding.imooc.com/class/280.html vue官网地址：https://cn.vuejs.org/">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902131845308.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902132239667.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902132123031.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902132420856.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902132627045.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902140626159.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902140928120.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902144549432.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902160126466.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902161506651.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902162915772.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902164605684.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902171249989.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902171405051.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190902181644548.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190903105243646.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190903111527024.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190903112140673.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190903112813954.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190903114520845.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190904122917158.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190903114942676.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190903120129489.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190904164226176.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190904165241959.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190909161809851.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190909162527862.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20190909173413287.jpg">
<meta property="og:updated_time" content="2019-09-24T00:58:33.497Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="【第二章】Vue全家桶+SSR+Koa2全栈开发美团网">
<meta name="twitter:description" content="本文为《Vue全家桶+SSR+Koa2全栈开发美团网》的学习笔记的第二章，主要内容为Vue基础知识视频地址：https://coding.imooc.com/class/280.html vue官网地址：https://cn.vuejs.org/">
<meta name="twitter:image" content="https://aiolos123.gitee.io/blog/images/20190902131845308.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://aiolos123.gitee.io/blog/2019/09/02/full-stack-dev-meituan-with-vue-two/">





  <title>【第二章】Vue全家桶+SSR+Koa2全栈开发美团网 | Aiolos</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Aiolos</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">记录我的成长点滴</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://aiolos123.gitee.io/blog/blog/2019/09/02/full-stack-dev-meituan-with-vue-two/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="aiolos">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Aiolos">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">【第二章】Vue全家桶+SSR+Koa2全栈开发美团网</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-02T12:01:33+08:00">
                2019-09-02
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Vue/" itemprop="url" rel="index">
                    <span itemprop="name">Vue</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本文为《Vue全家桶+SSR+Koa2全栈开发美团网》的学习笔记的第二章，主要内容为Vue基础知识<br>视频地址：<a href="https://coding.imooc.com/class/280.html" target="_blank" rel="noopener">https://coding.imooc.com/class/280.html</a></p>
<p>vue官网地址：<a href="https://cn.vuejs.org/" target="_blank" rel="noopener">https://cn.vuejs.org/</a></p>
<a id="more"></a>

<h1 id="搭建vue环境"><a href="#搭建vue环境" class="headerlink" title="搭建vue环境"></a>搭建vue环境</h1><h2 id="课程使用软件版本"><a href="#课程使用软件版本" class="headerlink" title="课程使用软件版本"></a>课程使用软件版本</h2><blockquote>
<p><a href="mailto:vue-cli@3.X" target="_blank" rel="noopener">vue-cli@3.X</a>,  <a href="mailto:vue@2.5" target="_blank" rel="noopener">vue@2.5</a></p>
</blockquote>
<h2 id="安装vue"><a href="#安装vue" class="headerlink" title="安装vue"></a>安装vue</h2><blockquote>
<p>安装vue之前要确保已经安装nodejs</p>
</blockquote>
<figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 全局安装最新版本的vue cli脚手架</span></span><br><span class="line"><span class="comment"># cnpm install -g <span class="doctag">@vue</span>/cli</span></span><br><span class="line"><span class="comment"># 全局安装与课程相同版本的的vue cli脚手架</span></span><br><span class="line">cnpm install -g @vue/cli@3.<span class="number">0</span>.<span class="number">1</span></span><br></pre></td></tr></table></figure>

<p><img src="/blog/images/20190902131845308.jpg" alt="全局安装特定版本的vue cli脚手架"></p>
<h2 id="创建vue项目"><a href="#创建vue项目" class="headerlink" title="创建vue项目"></a>创建vue项目</h2><figure class="highlight vala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">vue create vue项目名称</span><br><span class="line"><span class="meta"># 会弹出一个新窗口</span></span><br><span class="line"><span class="meta"># 选择使用默认的babel、eslint，回车</span></span><br><span class="line"><span class="meta"># 选择使用npm，回车</span></span><br></pre></td></tr></table></figure>

<p><img src="/blog/images/20190902132239667.jpg" alt="创建vue项目"><br><img src="/blog/images/20190902132123031.jpg" alt="创建vue项目"><br><img src="/blog/images/20190902132420856.jpg" alt="创建vue项目"></p>
<h2 id="启动项目"><a href="#启动项目" class="headerlink" title="启动项目"></a>启动项目</h2><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd  vue项目名称</span><br><span class="line">cnpm <span class="keyword">run</span><span class="bash"> serve</span></span><br></pre></td></tr></table></figure>

<p><img src="/blog/images/20190902132627045.jpg" alt="启动项目"></p>
<p>按照上面的提示，在浏览器中输入上述地址，即可看到vue的启动页面<br><img src="/blog/images/20190902140626159.jpg" alt="启动项目"></p>
<p><strong>至此，vue项目创建成功完成</strong></p>
<h1 id="vue项目目录结构"><a href="#vue项目目录结构" class="headerlink" title="vue项目目录结构"></a>vue项目目录结构</h1><p><img src="/blog/images/20190902140928120.jpg" alt="vue项目目录结构"></p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.git</span><br><span class="line">node_modules <span class="comment">//项目依赖模板</span></span><br><span class="line">public <span class="comment">//发布目录</span></span><br><span class="line">src <span class="comment">//源码</span></span><br><span class="line">babel<span class="selector-class">.config</span><span class="selector-class">.js</span> <span class="comment">//babel的配置文件</span></span><br></pre></td></tr></table></figure>

<h1 id="Vue基础"><a href="#Vue基础" class="headerlink" title="Vue基础"></a>Vue基础</h1><blockquote>
<p>本节中关于Vue的内容只服务于本视频，所以只讲到可用、看懂代码即可。<br>完整的Vue内容请到官网学习 Vue的官网：<a href="https://cn.vuejs.org/" target="_blank" rel="noopener">https://cn.vuejs.org/</a></p>
</blockquote>
<h2 id="模板语法"><a href="#模板语法" class="headerlink" title="模板语法"></a>模板语法</h2><p>本节对应官网地址：<a href="https://cn.vuejs.org/v2/guide/syntax.html" target="_blank" rel="noopener">https://cn.vuejs.org/v2/guide/syntax.html</a></p>
<blockquote>
<p>vue文件的三个组成部分：template(模板)、script、style</p>
</blockquote>
<p><img src="/blog/images/20190902144549432.jpg" alt="vue文件的三个组成部分"></p>
<p><strong>其中template部分是一个vue文件中必须包含的部分</strong></p>
<h3 id="显示纯文本"><a href="#显示纯文本" class="headerlink" title="显示纯文本"></a>显示纯文本</h3><figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml">//通过</span><span class="template-variable">&#123;&#123;&#125;&#125;</span><span class="xml">会将数据解释为普通文本</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">span</span>&gt;</span>Message:</span><span class="template-variable">&#123;&#123;msg&#125;&#125;</span><span class="xml"><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">export default &#123;</span></span><br><span class="line"><span class="xml">  name: 'HelloWorld',</span></span><br><span class="line"><span class="xml">  //两种定义msg的方式</span></span><br><span class="line"><span class="xml">  //props对象可以理解为传参入口</span></span><br><span class="line"><span class="xml">  props: &#123;</span></span><br><span class="line"><span class="xml">    msg: String</span></span><br><span class="line"><span class="xml">  &#125;,</span></span><br><span class="line"><span class="xml">  //在data函数中赋值</span></span><br><span class="line"><span class="xml">  data()&#123;</span></span><br><span class="line"><span class="xml">  	return &#123;</span></span><br><span class="line"><span class="xml">  		msg:"sss"</span></span><br><span class="line"><span class="xml">  	&#125;</span></span><br><span class="line"><span class="xml">  &#125;</span></span><br><span class="line"><span class="xml">&#125;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<h3 id="显示原始html"><a href="#显示原始html" class="headerlink" title="显示原始html"></a>显示原始html</h3><figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">//通过v-html指令输出真正的html</span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">p</span> <span class="attr">v-html</span>=<span class="string">'msg'</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">export default </span><span class="xquery">&#123;</span></span><br><span class="line"><span class="xquery"> <span class="built_in"> name</span>: <span class="string">'HelloWorld'</span>,</span></span><br><span class="line"><span class="xquery">  data()&#123;</span></span><br><span class="line"><span class="xquery">  	<span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="xquery">  		msg:<span class="string">"&lt;span style='color:red'&gt;Hello World&lt;/span&gt;"</span></span></span><br><span class="line"><span class="xquery">  	&#125;</span><span class="xml"></span></span><br><span class="line"><span class="xml">  &#125;</span></span><br><span class="line"><span class="xml">&#125;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<h3 id="特性"><a href="#特性" class="headerlink" title="特性"></a>特性</h3><figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml">//</span><span class="template-variable">&#123;&#123;&#125;&#125;</span><span class="xml">语法不能作用在 HTML 特性上，遇到这种情况应该使用v-bind指令</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">v-bind:id</span>=<span class="string">"dynamicld"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">export default &#123;</span></span><br><span class="line"><span class="xml">  name: 'HelloWorld',</span></span><br><span class="line"><span class="xml">  data()&#123;</span></span><br><span class="line"><span class="xml">  	return &#123;</span></span><br><span class="line"><span class="xml">  		dynamicld:"<span class="tag">&lt;<span class="name">span</span> <span class="attr">style</span>=<span class="string">'color:red'</span>&gt;</span>Hello World<span class="tag">&lt;/<span class="name">span</span>&gt;</span>"</span></span><br><span class="line"><span class="xml">  	&#125;</span></span><br><span class="line"><span class="xml">  &#125;</span></span><br><span class="line"><span class="xml">&#125;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<h3 id="表达式"><a href="#表达式" class="headerlink" title="表达式"></a>表达式</h3><figure class="highlight clojure"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123;number + <span class="number">1</span>&#125;&#125;</span><br><span class="line">&#123;&#123;ok ? 'Yes' : 'No'&#125;&#125;</span><br><span class="line">&#123;&#123;message.split(<span class="name">'</span> ').reverse().join(<span class="name">''</span>)&#125;&#125;</span><br></pre></td></tr></table></figure>

<h3 id="指令"><a href="#指令" class="headerlink" title="指令"></a>指令</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">//指令 (Directives) 是带有 v- 前缀的特殊特性。</span><br><span class="line">//指令特性的值预期是单个 JavaScript 表达式 (v-for 是例外情况，稍后我们再讨论)。</span><br><span class="line">//指令的职责是，当表达式的值改变时，将其产生的连带影响，响应式地作用于 DOM。</span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">v-if</span>=<span class="string">"seen"</span>&gt;</span>我看到你了<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">v-else</span>&gt;</span>你看不到我了<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="actionscript">  name: <span class="string">'HelloWorld'</span>,</span></span><br><span class="line">  data()&#123;</span><br><span class="line"><span class="actionscript">  	<span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="actionscript">  		seen:<span class="literal">true</span></span></span><br><span class="line">  	&#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="自定义指令"><a href="#自定义指令" class="headerlink" title="自定义指令"></a>自定义指令</h3><p>官网地址：<a href="https://cn.vuejs.org/v2/guide/custom-directive.html#ad" target="_blank" rel="noopener">https://cn.vuejs.org/v2/guide/custom-directive.html#ad</a></p>
<p>//1. 在n.js文件中注册一个全局自定义指令 <code>v-n</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line">Vue.directive(<span class="string">'n'</span>,&#123;</span><br><span class="line">	bind: <span class="function"><span class="keyword">function</span>(<span class="params">el,binding</span>)</span>&#123;</span><br><span class="line">		el.textContent=<span class="built_in">Math</span>.pow(binding.value,<span class="number">2</span>)</span><br><span class="line">	&#125;,</span><br><span class="line">	update:<span class="function"><span class="keyword">function</span>(<span class="params">el,binding</span>)</span>&#123;</span><br><span class="line">		el.textContent=<span class="built_in">Math</span>.pow(binding.value,<span class="number">2</span>)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><p>在App.vue中引入n.js文件</p>
<figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">'./components/n'</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在vue文件中调用指令</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">v-n</span>=<span class="string">'4'</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>4.显示效果<br><img src="/blog/images/20190902160126466.jpg" alt="显示效果"></p>
<h3 id="修饰符"><a href="#修饰符" class="headerlink" title="修饰符"></a>修饰符</h3><figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">//</span>修饰符 <span class="params">(modifier)</span> 是以半角句号 . 指明的特殊后缀，用于指出一个指令应该以特殊方式绑定。</span><br><span class="line"><span class="string">//</span>例如，<span class="string">.prevent</span> 修饰符告诉 v-on 指令对于触发的事件调用 event.preventDefault<span class="params">()</span> 阻止默认提交</span><br><span class="line">&lt;form v-on<span class="function">:submit.prevent</span>=<span class="string">"onSubmit"</span>&gt;<span class="string">...</span>&lt;<span class="string">/form</span>&gt;</span><br></pre></td></tr></table></figure>

<h3 id="缩写"><a href="#缩写" class="headerlink" title="缩写"></a>缩写</h3><blockquote>
<p>Vue 为 v-bind 和 v-on 这两个最常用的指令，提供了特定简写：</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">//v-bind 缩写</span><br><span class="line"><span class="comment">&lt;!-- 完整语法 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">v-bind:href</span>=<span class="string">"url"</span>&gt;</span>...<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 缩写 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">:href</span>=<span class="string">"url"</span>&gt;</span>...<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"></span><br><span class="line">//v-on 缩写</span><br><span class="line"><span class="comment">&lt;!-- 完整语法 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">v-on:click</span>=<span class="string">"doSomething"</span>&gt;</span>...<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 缩写 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> @<span class="attr">click</span>=<span class="string">"doSomething"</span>&gt;</span>...<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="计算属性"><a href="#计算属性" class="headerlink" title="计算属性"></a>计算属性</h2><p>计算属性的应用场景：应用于具有依赖关系的数据之间<br><img src="/blog/images/20190902161506651.jpg" alt="计算属性的应用场景"></p>
<h2 id="类与样式"><a href="#类与样式" class="headerlink" title="类与样式"></a>类与样式</h2><ol>
<li>对象语法：适用于较多的class或者动态的class</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1. 绑定的数据对象内联定义在模板里</span></span><br><span class="line">&lt;div</span><br><span class="line">  <span class="class"><span class="keyword">class</span></span>=<span class="string">"static"</span></span><br><span class="line">  v-bind:<span class="class"><span class="keyword">class</span></span>=<span class="string">"&#123; active: isActive, 'text-danger': hasError &#125;"</span></span><br><span class="line">&gt;<span class="xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line">data: &#123;</span><br><span class="line">  isActive: <span class="literal">true</span>,</span><br><span class="line">  hasError: <span class="literal">false</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//结果渲染为：</span></span><br><span class="line">&lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"static active"</span>&gt;<span class="xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//当 isActive 或者 hasError 变化时，class 列表将相应地更新。</span></span><br><span class="line"><span class="comment">//例如，如果 hasError 的值为 true，class 列表将变为 "static active text-danger"。</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//2. 绑定的数据对象不必内联定义在模板里</span></span><br><span class="line">&lt;div v-bind:<span class="class"><span class="keyword">class</span></span>=<span class="string">"classObject"</span>&gt;<span class="xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line">data: &#123;</span><br><span class="line">  classObject: &#123;</span><br><span class="line">    active: <span class="literal">true</span>,</span><br><span class="line">    <span class="string">'text-danger'</span>: <span class="literal">false</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//结果渲染为：</span></span><br><span class="line">&lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"static active"</span>&gt;<span class="xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>数组语法：适用于较少的class<br><img src="/blog/images/20190902162915772.jpg" alt="数组语法"></li>
</ol>
<h2 id="条件与列表渲染"><a href="#条件与列表渲染" class="headerlink" title="条件与列表渲染"></a>条件与列表渲染</h2><p>基础用法：v-if v-else、v-for的模板处理方式<br>详情见：<a href="https://cn.vuejs.org/v2/guide/conditional.html" target="_blank" rel="noopener">https://cn.vuejs.org/v2/guide/conditional.html</a></p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//可以用 v-for 指令基于一个数组来渲染一个列表。</span></span><br><span class="line"><span class="comment">//v-for 指令需要使用 item in items 形式的特殊语法，</span></span><br><span class="line"><span class="comment">//其中 items 是源数据数组，而 item 则是被迭代的数组元素的别名。</span></span><br><span class="line">&lt;<span class="selector-tag">ul</span> id=<span class="string">"example-1"</span>&gt;</span><br><span class="line">  &lt;<span class="selector-tag">li</span> v-<span class="keyword">for</span>=<span class="string">"(item,index) in items"</span>&gt;</span><br><span class="line">    &#123;&#123; item<span class="selector-class">.message</span> &#125;&#125;</span><br><span class="line">  &lt;/li&gt;</span><br><span class="line">&lt;/ul&gt;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">var</span> example1 = new Vue(&#123;</span><br><span class="line">  el: <span class="string">'#example-1'</span>,</span><br><span class="line">  data: &#123;</span><br><span class="line">    items: [</span><br><span class="line">      &#123; message: <span class="string">'Foo'</span> &#125;,</span><br><span class="line">      &#123; message: <span class="string">'Bar'</span> &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//输出结果</span></span><br><span class="line">&lt;li&gt;Foo&lt;/li&gt;</span><br><span class="line">&lt;li&gt;Bar&lt;/li&gt;</span><br></pre></td></tr></table></figure>

<p>分组用法： 复杂模板的处理方式<br><img src="/blog/images/20190902164605684.jpg" alt="分组用法"></p>
<h2 id="事件处理"><a href="#事件处理" class="headerlink" title="事件处理"></a>事件处理</h2><ol>
<li><p>事件定义与缩写</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//可以用 v-on 指令监听 DOM 事件，并在触发时运行一些 JavaScript 代码或方法</span></span><br><span class="line">&lt;div id=<span class="string">"example-2"</span>&gt;</span><br><span class="line">  &lt;!-- <span class="string">`greet`</span> 是在下面定义的方法名 --&gt;</span><br><span class="line">  &lt;button v-on:click=<span class="string">"greet"</span>&gt;Greet&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>div&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">	methods: &#123;</span><br><span class="line">       greet: <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;</span><br><span class="line">          <span class="comment">// `event` 是原生 DOM 事件</span></span><br><span class="line">          <span class="keyword">if</span> (event) &#123;</span><br><span class="line">             alert(event.target.tagName)</span><br><span class="line">          &#125;</span><br><span class="line">       &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>事件内联写法(传参)</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=<span class="string">"example-2"</span>&gt;</span><br><span class="line">  &lt;!-- <span class="string">`greet`</span> 是在下面定义的方法名</span><br><span class="line">   还可以传入参数、事件对象</span><br><span class="line">  --&gt;</span><br><span class="line">  &lt;button v-on:click=<span class="string">"greet('sss',$event)"</span>&gt;Greet&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>div&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">	methods: &#123;</span><br><span class="line">       greet: <span class="function"><span class="keyword">function</span> (<span class="params">name,event</span>) </span>&#123;</span><br><span class="line">       	  <span class="built_in">window</span>.console.log(name);</span><br><span class="line">          <span class="comment">// `event` 是原生 DOM 事件</span></span><br><span class="line">          <span class="keyword">if</span> (event) &#123;</span><br><span class="line">             alert(event.target.tagName)</span><br><span class="line">          &#125;</span><br><span class="line">       &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>事件修饰符</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 阻止单击事件继续传播(即阻止事件冒泡) --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">v-on:click.stop</span>=<span class="string">"doThis"</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"></span><br><span class="line">......</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>事件冒泡的示例：<br><img src="/blog/images/20190902171249989.jpg" alt="事件冒泡"></p>
<p>通过事件修饰符来阻止事件冒泡：<br><img src="/blog/images/20190902171405051.jpg" alt="阻止事件冒泡"></p>
<ol start="4">
<li>快速结合键盘事件提升效率</li>
</ol>
<h2 id="深入了解组件"><a href="#深入了解组件" class="headerlink" title="深入了解组件"></a>深入了解组件</h2><blockquote>
<p>vue是MVVM框架，一切都是基于组件开发的</p>
</blockquote>
<h3 id="组件知识点总结："><a href="#组件知识点总结：" class="headerlink" title="组件知识点总结："></a>组件知识点总结：</h3><blockquote>
<p>a. 一个vue文件就是一个组件，包括三部分：template(模板)、script、style<br>。并且在template下只能有一个dom节点。组件可以复用。<br>b. 一个组件的 data 选项必须是一个函数<br>c. 通过prop父组件向子组件传递数据<br>d. 通过事件子组件向父级组件传递数据<br>e. slot 插槽在组件抽象设计中的应用<br>f. 通过vuex实现跨组件通信</p>
</blockquote>
<h3 id="通过prop从父组件向子组件传递数据"><a href="#通过prop从父组件向子组件传递数据" class="headerlink" title="通过prop从父组件向子组件传递数据"></a>通过prop从父组件向子组件传递数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//父组件 app.vue</span></span><br><span class="line">&lt;com :age=<span class="string">'ages'</span>/&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com <span class="keyword">from</span> <span class="string">'./components/com.vue'</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">'app'</span>,</span><br><span class="line">  components: &#123;</span><br><span class="line">	com</span><br><span class="line">  &#125;,</span><br><span class="line">  data()&#123;</span><br><span class="line">	  <span class="keyword">return</span> &#123;</span><br><span class="line">		  ages: <span class="number">184</span></span><br><span class="line">	  &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//子组件中使用 com.vue</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">	&lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"com"</span>&gt;</span><br><span class="line">		child components</span><br><span class="line">		&#123;&#123;name&#125;&#125; &#123;&#123;age&#125;&#125;</span><br><span class="line">	&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>template&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">	    <span class="comment">//通过prop从父组件向子组件传递数据age</span></span><br><span class="line">		props:[<span class="string">'age'</span>],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出结果如下图：<br><img src="/blog/images/20190902181644548.jpg" alt="通过prop从父组件向子组件传递数据"></p>
<h3 id="通过事件从子组件向父级组件传递数据"><a href="#通过事件从子组件向父级组件传递数据" class="headerlink" title="通过事件从子组件向父级组件传递数据"></a>通过事件从子组件向父级组件传递数据</h3><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//子组件中编写自定义事件 com.vue ———— 在事件中通过$emit()实现子组件向父组件传递数据</span></span><br><span class="line">&lt;button type=<span class="string">"button"</span> name=<span class="string">"button"</span> <span class="meta">@click</span>=<span class="string">"<span class="variable">$emit</span>('patch',34)"</span>&gt;发送到父组件&lt;/button&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//父组件接收子组件事件和数据 App.vue</span></span><br><span class="line">&lt;com :age=<span class="string">'ages'</span> <span class="meta">@patch</span>=<span class="string">"msg"</span>/&gt;</span><br><span class="line">methods:&#123;</span><br><span class="line">	  <span class="keyword">data</span>()&#123;</span><br><span class="line">	    <span class="keyword">return</span> &#123;</span><br><span class="line">		   ages: <span class="number">184</span></span><br><span class="line">	    &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      methods:&#123;</span><br><span class="line">	     msg:function(aj)&#123;</span><br><span class="line">		    <span class="keyword">this</span>.ages ++</span><br><span class="line">		    window.console.log(aj) <span class="comment">//34</span></span><br><span class="line">	     &#125;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="/blog/images/20190903105243646.jpg" alt="通过事件从子组件向父级组件传递数据"></p>
<p>数据传递的完整流程：</p>
<blockquote>
<p>父组件ages(184) –&gt; props –&gt; 子组件 –&gt; $emit()  –&gt; 父组件(age++)</p>
</blockquote>
<h3 id="slot插槽"><a href="#slot插槽" class="headerlink" title="slot插槽"></a>slot插槽</h3><blockquote>
<p>使用场景：抽象设计组件达到动态改变组件的时候</p>
</blockquote>
<ol>
<li>单个插槽的用法<figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml">//父组件</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">com</span> <span class="attr">:age</span>=<span class="string">'ages'</span> @<span class="attr">patch</span>=<span class="string">"msg"</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">h1</span>&gt;</span>我要加东西<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">com</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">//子组件</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"com"</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">span</span>&gt;</span> child components</span></span><br><span class="line"><span class="xml">		</span><span class="template-variable">&#123;&#123;name&#125;&#125;</span><span class="xml"> </span><span class="template-variable">&#123;&#123;age&#125;&#125;</span><span class="xml"><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">name</span>=<span class="string">"button"</span> @<span class="attr">click</span>=<span class="string">"$emit('patch')"</span>&gt;</span>发送到父组件<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">slot</span>&gt;</span><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>实现效果如下图：<br><img src="/blog/images/20190903111527024.jpg" alt="单个插槽的用法"></p>
<ol start="2">
<li>多个插槽以name属性区分<figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml">//父组件</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">com</span> <span class="attr">:age</span>=<span class="string">'ages'</span> @<span class="attr">patch</span>=<span class="string">"msg"</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">h1</span> <span class="attr">slot</span>=<span class="string">"bslot"</span>&gt;</span>我也要加东西<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">h1</span> <span class="attr">slot</span>=<span class="string">"aslot"</span>&gt;</span>我要加东西<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">com</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">//子组件</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"com"</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">slot</span> <span class="attr">name</span>=<span class="string">"bslot"</span>&gt;</span><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">span</span>&gt;</span> child components</span></span><br><span class="line"><span class="xml">		</span><span class="template-variable">&#123;&#123;name&#125;&#125;</span><span class="xml"> </span><span class="template-variable">&#123;&#123;age&#125;&#125;</span><span class="xml"><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">name</span>=<span class="string">"button"</span> @<span class="attr">click</span>=<span class="string">"$emit('patch')"</span>&gt;</span>发送到父组件<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="tag">&lt;<span class="name">slot</span> <span class="attr">name</span>=<span class="string">"aslot"</span>&gt;</span><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>实现效果如下图：<br><img src="/blog/images/20190903112140673.jpg" alt="多个插槽的用法"></p>
<h2 id="路由基础"><a href="#路由基础" class="headerlink" title="路由基础"></a>路由基础</h2><blockquote>
<p>凡涉及到vue做SPA都会用到路由</p>
</blockquote>
<p>vue-router的官网：<a href="https://router.vuejs.org/zh/" target="_blank" rel="noopener">https://router.vuejs.org/zh/</a></p>
<h3 id="路由的通常用法-90-的用法都是如下代码"><a href="#路由的通常用法-90-的用法都是如下代码" class="headerlink" title="路由的通常用法(90%的用法都是如下代码)"></a>路由的通常用法(90%的用法都是如下代码)</h3><p>见：<a href="https://router.vuejs.org/zh/guide/#html" target="_blank" rel="noopener">https://router.vuejs.org/zh/guide/#html</a></p>
<ol>
<li>安装vue-router<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> vue项目</span><br><span class="line">npm install vue-router</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p><img src="/blog/images/20190903112813954.jpg" alt="安装路由"></p>
<ol start="2">
<li><p>配置路由<br><img src="/blog/images/20190903114520845.jpg" alt="创建路由"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//router.js</span></span><br><span class="line"><span class="comment">//导入页面文件</span></span><br><span class="line"><span class="keyword">import</span> pageA <span class="keyword">from</span> <span class="string">'./pages/a.vue'</span></span><br><span class="line"><span class="keyword">import</span> pageB <span class="keyword">from</span> <span class="string">'./pages/b.vue'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//导入vue和vue-router</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> VueRouter <span class="keyword">from</span> <span class="string">'vue-router'</span></span><br><span class="line"><span class="comment">//通过 Vue.use() 明确地安装路由功能</span></span><br><span class="line">Vue.use(VueRouter)</span><br><span class="line"></span><br><span class="line"><span class="comment">//路由的三步配置</span></span><br><span class="line"><span class="comment">//1. 配置路由、页面路径与组件关联关系</span></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line">	&#123;<span class="attr">path</span>: <span class="string">'/'</span>,<span class="attr">component</span>:pageA&#125;, <span class="comment">//默认页面</span></span><br><span class="line">	&#123;<span class="attr">path</span>: <span class="string">'/pageb'</span>,<span class="attr">component</span>:pageB&#125;,</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="comment">//2. 实例化VueRouter，生效关联关系</span></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">	routes</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//3. 导出对象</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> router</span><br></pre></td></tr></table></figure>
</li>
<li><p>关联项目与路由实例</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//main.js</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">'./router'</span></span><br><span class="line"></span><br><span class="line">Vue.config.productionTip = <span class="keyword">false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  <span class="comment">//通过路由启动页面，关联路由与项目</span></span><br><span class="line">  router</span><br><span class="line">&#125;).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>在html文件中通过router-view来进行路由渲染页面</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 1. 路由出口 --&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 路由匹配到的组件将渲染在这里 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">router-view</span>&gt;</span><span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 2. 使用 router-link 组件来导航. --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 通过传入 `to` 属性指定链接. --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- &lt;router-link&gt; 默认会被渲染成一个 `&lt;a&gt;` 标签 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/"</span>&gt;</span>Go to Foo<span class="tag">&lt;/<span class="name">router-link</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/pageb"</span>&gt;</span>Go to Bar<span class="tag">&lt;/<span class="name">router-link</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>页面显示路由如下图</p>
<blockquote>
<p><strong><em>路由匹配到的组件将渲染在router-view标签中</em></strong></p>
</blockquote>
</li>
</ol>
<p><img src="/blog/images/20190904122917158.jpg" alt="页面显示路由"></p>
<h3 id="常见错误"><a href="#常见错误" class="headerlink" title="常见错误"></a>常见错误</h3><ol>
<li>页面空白，浏览器报错[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. </li>
</ol>
<p><img src="/blog/images/20190903114942676.jpg" alt="页面空白，浏览器报错"></p>
<p>错误原因： 不再从App.vue启动页面，只包含运行时，没有编译器导致模板无法编译<br><img src="/blog/images/20190903120129489.jpg" alt="错误原因"><br>解决方法： 在vue.config.js中增加vue-cli关于运行时启动编译器的配置<br>vue-cli官网：<a href="https://cli.vuejs.org/zh/guide/" target="_blank" rel="noopener">https://cli.vuejs.org/zh/guide/</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">module</span>.<span class="keyword">exports</span> = &#123;</span><br><span class="line">	runtimeCompiler: <span class="keyword">true</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>router-view不显示内容<br>原因： 首先查看浏览器是否报错，如果没有报错，则查看配置路由的代码，<br>尤其是对象的key的名称是否正确，本人就是因为component写错导致router-view不显示内容</li>
</ol>
<h2 id="Vuex基础"><a href="#Vuex基础" class="headerlink" title="Vuex基础"></a>Vuex基础</h2><p>Vuex官网：<a href="https://vuex.vuejs.org/zh/" target="_blank" rel="noopener">https://vuex.vuejs.org/zh/</a><br>Vuex作用： 跨组件的数据通信、共享</p>
<blockquote>
<p>state: 所有组件之间共享的数据，也可以render到Vue组件中<br>mutations：如何改变state数据<br>actions：什么时候改变state数据，在Vue组件中通过用户交互触发actions</p>
</blockquote>
<p><img src="/blog/images/20190904164226176.jpg" alt="Vuex核心概念"></p>
<h3 id="Vuex的基础用法"><a href="#Vuex的基础用法" class="headerlink" title="Vuex的基础用法"></a>Vuex的基础用法</h3><ol>
<li>安装Vuex<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> vue项目</span><br><span class="line">npm install vuex</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p><img src="/blog/images/20190904165241959.jpg" alt="安装Vuex"></p>
<ol start="2">
<li><p>编写vuex示例</p>
<figure class="highlight pf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">//store.js</span><br><span class="line">import Vue <span class="keyword">from</span> 'vue';</span><br><span class="line">import Vuex <span class="keyword">from</span> 'vuex';</span><br><span class="line">//通过 Vue.use() 来安装 Vuex</span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line">//<span class="number">1</span>. 声明<span class="keyword">state</span></span><br><span class="line">const <span class="keyword">state</span> = &#123;</span><br><span class="line">	count: <span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//<span class="number">2</span>. 定义mutations,在mutation中修改<span class="keyword">state</span></span><br><span class="line">const mutations = &#123;</span><br><span class="line">	increment(<span class="keyword">state</span>)&#123;</span><br><span class="line">		<span class="keyword">state</span>.count++</span><br><span class="line">	&#125;,</span><br><span class="line">	decrement(<span class="keyword">state</span>)&#123;</span><br><span class="line">		<span class="keyword">state</span>.count--</span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">//<span class="number">3</span>. 定义actions，接收vue组件的用户行为</span><br><span class="line">const actions = &#123;</span><br><span class="line">	increment:(&#123;commit&#125;) =&gt; &#123;</span><br><span class="line">		commit('increment')</span><br><span class="line">	&#125;,</span><br><span class="line">	decrement: (&#123;commit&#125;) =&gt; &#123;</span><br><span class="line">		commit('decrement')</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//<span class="number">4</span>. 导出</span><br><span class="line">export <span class="keyword">default</span> new Vuex.Store(&#123;</span><br><span class="line">	<span class="keyword">state</span>,</span><br><span class="line">	mutations,</span><br><span class="line">	actions</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>在应用中加载vuex示例</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//main.js</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">'./App.vue'</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store'</span></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">  store</span><br><span class="line">&#125;).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>加载vuex组件到应用中</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//vuex.vue</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">	&lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"vuex"</span>&gt;</span><br><span class="line">		vuexx</span><br><span class="line">	&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>template&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">//App.vue</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div id=<span class="string">"app"</span>&gt;</span><br><span class="line">	&lt;vuexx/&gt;</span><br><span class="line">  &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>template&gt;</span><br><span class="line"><span class="keyword">import</span> vuexx <span class="keyword">from</span> <span class="string">'./components/vuex.vue'</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">'app'</span>,</span><br><span class="line">  components: &#123;</span><br><span class="line">	vuexx</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p><img src="/blog/images/20190909161809851.jpg" alt="在应用中加载vuex示例"></p>
<ol start="5">
<li>在vuex组件中使用state<figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">//vuex.vue</span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span></span><br><span class="line"><span class="xml">	</span><span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"vuex"</span>&gt;</span></span></span><br><span class="line"><span class="xml">		<span class="comment">&lt;!-- </span></span></span><br><span class="line"><span class="xml">			1. $store为main.js中加载vuex示例的store</span></span><br><span class="line"><span class="xml">			2. state为store.js中声明state</span></span><br><span class="line"><span class="xml">		 --&gt;</span></span><br><span class="line"><span class="xml">		vuexx </span><span class="xquery">&#123;&#123;<span class="variable">$store</span>.state<span class="built_in">.count</span>&#125;</span><span class="xml">&#125;</span></span><br><span class="line"><span class="xml">		</span><span class="xml"><span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span> @<span class="attr">click</span>=<span class="string">"increment"</span>&gt;</span>增加<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><span class="xml"></span></span><br><span class="line"><span class="xml">		</span><span class="xml"><span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span> @<span class="attr">click</span>=<span class="string">"decrement"</span>&gt;</span>删减<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><span class="xml"></span></span><br><span class="line"><span class="xml">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><span class="xml"></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">	//mapActions的作用： 在组件中分为action</span></span><br><span class="line"><span class="xml">	import </span><span class="xquery">&#123; mapActions &#125;</span><span class="xml"> from 'vuex' </span></span><br><span class="line"><span class="xml">	export default </span><span class="xquery">&#123;</span></span><br><span class="line"><span class="xquery">		methods: mapActions([</span></span><br><span class="line"><span class="xquery">			<span class="string">'increment'</span>,</span></span><br><span class="line"><span class="xquery">			<span class="string">'decrement'</span></span></span><br><span class="line"><span class="xquery">		])</span></span><br><span class="line"><span class="xquery">	&#125;</span><span class="xml"></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

</li>
</ol>
<p><img src="/blog/images/20190909162527862.jpg" alt="在vuex组件中使用state"></p>
<h3 id="Vuex的高级用法"><a href="#Vuex的高级用法" class="headerlink" title="Vuex的高级用法"></a>Vuex的高级用法</h3><ol>
<li>不同页面管理不同的state<br>i. 声明模块<figure class="highlight pf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">//定义a.vue和b.vue</span><br><span class="line"><span class="variable">&lt;template&gt;</span></span><br><span class="line">	<span class="variable">&lt;div class="page"&gt;</span></span><br><span class="line">		page a &#123;&#123; <span class="variable">$store</span>.<span class="keyword">state</span>.money.money &#125;&#125;</span><br><span class="line">		<span class="variable">&lt;button type="button" @click="add"&gt;</span>增加&lt;/button&gt;</span><br><span class="line">		<span class="variable">&lt;button type="button" @click="reduce"&gt;</span>删减&lt;/button&gt;</span><br><span class="line">	&lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line"><span class="variable">&lt;script&gt;</span></span><br><span class="line">	import &#123; mapActions &#125; <span class="keyword">from</span> 'vuex'</span><br><span class="line">	export <span class="keyword">default</span> &#123;</span><br><span class="line">		methods: mapActions('money',['add','reduce'])</span><br><span class="line">	&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line"><span class="variable">&lt;style&gt;</span></span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br><span class="line">//对应的定义a.js和b.js</span><br><span class="line">const <span class="keyword">state</span> = &#123;</span><br><span class="line">	money: <span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const mutations = &#123;</span><br><span class="line">	add(<span class="keyword">state</span>)&#123;</span><br><span class="line">		<span class="keyword">state</span>.money++</span><br><span class="line">	&#125;,</span><br><span class="line">	reduce(<span class="keyword">state</span>)&#123;</span><br><span class="line">		<span class="keyword">state</span>.money--</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const actions = &#123;</span><br><span class="line">	add: (&#123;commit&#125;) =&gt; &#123;</span><br><span class="line">		commit('add')</span><br><span class="line">	&#125;,</span><br><span class="line">	reduce: (&#123;commit&#125;) =&gt; &#123;</span><br><span class="line">		commit('reduce')</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export <span class="keyword">default</span> &#123;</span><br><span class="line">	namespaced: true,</span><br><span class="line">	<span class="keyword">state</span>,</span><br><span class="line">	mutations,</span><br><span class="line">	actions</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>ii. 导出模块实例index.js</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">'vuex'</span></span><br><span class="line"><span class="keyword">import</span> money <span class="keyword">from</span> <span class="string">'./modules/a'</span></span><br><span class="line"><span class="keyword">import</span> <span class="keyword">count</span> <span class="keyword">from</span> <span class="string">'./modules/b'</span></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"><span class="comment">//导出实例</span></span><br><span class="line">export <span class="keyword">default</span> <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">	modules:&#123;</span><br><span class="line">		money,</span><br><span class="line">		<span class="keyword">count</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>iii. 加载模块到应用中</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//main.js</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store/index'</span></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">  <span class="comment">//router,</span></span><br><span class="line">  store</span><br><span class="line">&#125;).$mount(<span class="string">'#app'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//App.vue</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div id=<span class="string">"app"</span>&gt;</span><br><span class="line">	&lt;pagea/&gt;</span><br><span class="line">	&lt;pageb/&gt;</span><br><span class="line">  &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>template&gt;</span><br><span class="line"><span class="keyword">import</span> pagea <span class="keyword">from</span> <span class="string">'./components/a.vue'</span></span><br><span class="line"><span class="keyword">import</span> pageb <span class="keyword">from</span> <span class="string">'./components/b.vue'</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">'app'</span>,</span><br><span class="line">  components: &#123;</span><br><span class="line">	pagea,</span><br><span class="line">	pageb</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="/blog/images/20190909173413287.jpg" alt="不同页面管理不同的state"></p>
<ol start="2">
<li>从外表传参<figure class="highlight pf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">//b.vue</span><br><span class="line"><span class="variable">&lt;template&gt;</span></span><br><span class="line">	<span class="variable">&lt;div class="page"&gt;</span></span><br><span class="line">		page b &#123;&#123;<span class="variable">$store</span>.<span class="keyword">state</span>.count.count&#125;&#125;</span><br><span class="line">		<span class="variable">&lt;button type="button" name="btn" @click="add(2)"&gt;</span>增加&lt;/button&gt;</span><br><span class="line">		<span class="variable">&lt;button type="button" name="btn" @click="reduce(2)"&gt;</span>删减&lt;/button&gt;</span><br><span class="line">	&lt;/div&gt;</span><br><span class="line">	</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">//b.js</span><br><span class="line">const <span class="keyword">state</span> = &#123;</span><br><span class="line">	count: <span class="number">10</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const mutations = &#123;</span><br><span class="line">	add(<span class="keyword">state</span>,param)&#123;</span><br><span class="line">		<span class="keyword">state</span>.count+=param</span><br><span class="line">	&#125;,</span><br><span class="line">	reduce(<span class="keyword">state</span>,param)&#123;</span><br><span class="line">		<span class="keyword">state</span>.count-=param</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">//传递参数</span><br><span class="line">const actions = &#123;</span><br><span class="line">	add: (&#123;commit&#125;,param) =&gt; &#123;</span><br><span class="line">		commit('add',param)</span><br><span class="line">	&#125;,</span><br><span class="line">	reduce: (&#123;commit&#125;,param) =&gt; &#123;</span><br><span class="line">		commit('reduce',param)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Vue全栈开发美团网/" rel="tag"># Vue全栈开发美团网</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/09/02/full-stack-dev-meituan-with-vue-one/" rel="next" title="【第一章】Vue全家桶+SSR+Koa2全栈开发美团网">
                <i class="fa fa-chevron-left"></i> 【第一章】Vue全家桶+SSR+Koa2全栈开发美团网
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2019/09/09/full-stack-dev-meituan-with-vue-three/" rel="prev" title="【第三章】Vue全家桶+SSR+Koa2全栈开发美团网">
                【第三章】Vue全家桶+SSR+Koa2全栈开发美团网 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">aiolos</p>
              <p class="site-description motion-element" itemprop="description">Java Spring Hadoop 机器学习</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">126</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">33</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#搭建vue环境"><span class="nav-number">1.</span> <span class="nav-text">搭建vue环境</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#课程使用软件版本"><span class="nav-number">1.1.</span> <span class="nav-text">课程使用软件版本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装vue"><span class="nav-number">1.2.</span> <span class="nav-text">安装vue</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建vue项目"><span class="nav-number">1.3.</span> <span class="nav-text">创建vue项目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#启动项目"><span class="nav-number">1.4.</span> <span class="nav-text">启动项目</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#vue项目目录结构"><span class="nav-number">2.</span> <span class="nav-text">vue项目目录结构</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Vue基础"><span class="nav-number">3.</span> <span class="nav-text">Vue基础</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#模板语法"><span class="nav-number">3.1.</span> <span class="nav-text">模板语法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#显示纯文本"><span class="nav-number">3.1.1.</span> <span class="nav-text">显示纯文本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#显示原始html"><span class="nav-number">3.1.2.</span> <span class="nav-text">显示原始html</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#特性"><span class="nav-number">3.1.3.</span> <span class="nav-text">特性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#表达式"><span class="nav-number">3.1.4.</span> <span class="nav-text">表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#指令"><span class="nav-number">3.1.5.</span> <span class="nav-text">指令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义指令"><span class="nav-number">3.1.6.</span> <span class="nav-text">自定义指令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修饰符"><span class="nav-number">3.1.7.</span> <span class="nav-text">修饰符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#缩写"><span class="nav-number">3.1.8.</span> <span class="nav-text">缩写</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#计算属性"><span class="nav-number">3.2.</span> <span class="nav-text">计算属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类与样式"><span class="nav-number">3.3.</span> <span class="nav-text">类与样式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#条件与列表渲染"><span class="nav-number">3.4.</span> <span class="nav-text">条件与列表渲染</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事件处理"><span class="nav-number">3.5.</span> <span class="nav-text">事件处理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#深入了解组件"><span class="nav-number">3.6.</span> <span class="nav-text">深入了解组件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#组件知识点总结："><span class="nav-number">3.6.1.</span> <span class="nav-text">组件知识点总结：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#通过prop从父组件向子组件传递数据"><span class="nav-number">3.6.2.</span> <span class="nav-text">通过prop从父组件向子组件传递数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#通过事件从子组件向父级组件传递数据"><span class="nav-number">3.6.3.</span> <span class="nav-text">通过事件从子组件向父级组件传递数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#slot插槽"><span class="nav-number">3.6.4.</span> <span class="nav-text">slot插槽</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#路由基础"><span class="nav-number">3.7.</span> <span class="nav-text">路由基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#路由的通常用法-90-的用法都是如下代码"><span class="nav-number">3.7.1.</span> <span class="nav-text">路由的通常用法(90%的用法都是如下代码)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常见错误"><span class="nav-number">3.7.2.</span> <span class="nav-text">常见错误</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Vuex基础"><span class="nav-number">3.8.</span> <span class="nav-text">Vuex基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Vuex的基础用法"><span class="nav-number">3.8.1.</span> <span class="nav-text">Vuex的基础用法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Vuex的高级用法"><span class="nav-number">3.8.2.</span> <span class="nav-text">Vuex的高级用法</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">aiolos</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/blog/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
